<?php
class CMRPageEditResource extends CMRPage

{
	private $resourceID;
	
	public function __construct($database)
	{
		parent::__construct($database);
		if($this->getAction()=='UpdateRecord')
		{
			$callback = new DataTableCallback($this->Database->Recopy(), $this->UserID);
			$callback->updateRecord($_POST['DBTableName']);
		}
	}
	


protected function getResources()
	{
		$resources = array();
		
		$sql = "SELECT * FROM Resources WHERE DeletedBy = '0' AND StatusTypeID = '1' ORDER BY ResourceTitle";
		$this->Database->ExecuteSQL($sql);
		while($row = $this->Database->getRow())
		{
			$resource = new Resource($this->Database->Recopy(),$row['ResourceID']);
//			$resource->loadData($row['ResourceID'],$row['ResourceTitle'],$row['ResourceDescription'],$row['ResourceTypeID'],"","",,,,);
			$resource->setCurrentUserGroup($this->userGroup);
			$resource->setDisplayMode('edit');
			array_push($resources,$resource);
		}
				
		return $resources;
	}

	
	protected function getContentsDiv()
	{
		
		$category = new Hidden();
		$category->setName("ResourceID");
		$html = $category->getHTML();
	
		$html .= "<div id=\"content\">\n";
		$html .= "<h1>Edit a Resource</h1>";

		$hidden = new Hidden();
		$hidden->setName("SubAction");
		$html .= $hidden->getHTML();
		
		$script = new EditDeleteFormScript();
		$html .= $script->getHTML();
		
		switch ($this->getSubAction())
		{
			case 'EditResource':
				$html .= $this->getEditResourceHTML();
				break;
			case 'RemoveResource';
				$html .= $this->getRemoveResourceHTML();
				break;
			
			default:
				$html .= $this->getDefault();			
				break;
		}
		

		$html .= "</div>";
		return $html;
				
	}
	
	private function getDefault()
	{
		$html = "<p>Select the resource you wish to edit from the  list below.</p>";

		$resources = $this->getResources();
		foreach ($resources as $resource)
		{
			$html .= $resource->getSummaryHTML();
		}
				
/*		$button = new JavascriptButton();
		$button->setValue("Next");
		$button->setJavascript("var submitaction; submitaction='submitCreate'+(document.getElementById('SubmitType').options[document.getElementById('SubmitType').selectedIndex].text); document.getElementById('Action').value = 'adminCreateResource'; document.getElementById('SubAction').value = submitaction; document.getElementById('PageForm').submit();");
		$html .= $button->getHTML();
		*/
		return $html;
		
	}

	private function getEditResourceHTML()
	{
		$this->resourceID = $_POST['ResourceID'];
		$resource = new Resource($this->Database, $this->resourceID);
		$html = '';
//		echo "Resource type = ".$resource->getResourceTypeID();
		switch ($resource->getResourceTypeID())
		{
			case 2:		// repertoire item
				$html = $this->getEditRepertoire();
				break;
				
			case 3:		// composer
				$html = $this->getEditComposer();
				break;
				
			case 6:		// file
				$html = $this->getEditFile();
				break;
				
			case 7:		// link
				$html = $this->getEditLink();
				break;
		}
		return $html;
		
	}
	
	private function getRemoveResourceHTML()
	{
		$this->resourceID = $_POST['ResourceID'];
		$resource = new Resource($this->Database, $this->resourceID);
		$html = '';
//		echo "Resource type = ".$resource->getResourceTypeID();

		$sql = "UPDATE Resources SET StatusTypeID='4' WHERE ResourceID=".$this->resourceID;
		$this->Database->ExecuteSQL($sql);
		$html .= "<p>Resource is now removed</p>";
				
		return $html;
		
	}
	
	
	
	private function getEditFile()
	{
		$hidden = new Hidden();
		$hidden->setName("RecordID");
		$html = $hidden->getHTML();
		
		$hidden = new Hidden();
		$hidden->setName("DBTableName");
		$html .= $hidden->getHTML();
		
		$script = new UpdateRecordScript();
		$html .= $script->getHTML();
		$panel = new PanelEditDBRecord($this->Database,'Resources',$this->resourceID);
		$panel->addReadOnlyField('StatusTypeID');
		$panel->setIncludeCommentField(true);
		$panel->setParentPage($this);
		$panel->setTypeTable('ResourceTypeID','Files',6);
		$html .= $panel->getHTML();
		return $html;
		
	}
	
	private function getEditComposer()
	{
		$hidden = new Hidden();
		$hidden->setName("RecordID");
		$html = $hidden->getHTML();
		
		$hidden = new Hidden();
		$hidden->setName("DBTableName");
		$html .= $hidden->getHTML();
		
		$script = new UpdateRecordScript();
		$html .= $script->getHTML();
		$panel = new PanelEditDBRecord($this->Database,'Resources',$this->resourceID);
		$panel->addReadOnlyField('StatusTypeID');
//		echo "From getEditComposer - setIncludeCommentField<br>\n";
		$panel->setIncludeCommentField(true);
		$panel->setParentPage($this);
		$panel->setTypeTable('ResourceTypeID','Composers',3);
		$panel->setYearRange(1200,2020);
		$panel->setDateDisabler(true);
		$html .= $panel->getHTML();
		return $html;
	}
	
	private function getEditLink()
	{
		$hidden = new Hidden();
		$hidden->setName("RecordID");
		$html = $hidden->getHTML();
		
		$hidden = new Hidden();
		$hidden->setName("DBTableName");
		$html .= $hidden->getHTML();
		
		$script = new UpdateRecordScript();
		$html .= $script->getHTML();
		$panel = new PanelEditDBRecord($this->Database,'Resources',$this->resourceID);
		$panel->addReadOnlyField('StatusTypeID');
		$panel->setIncludeCommentField(true);
		$panel->setParentPage($this);
		$panel->setTypeTable('ResourceTypeID','Links',7);
//		$panel->setYearRange(1400,2020);
		$html .= $panel->getHTML();
		return $html;
		
		
	}
	
	private function getEditRepertoire()
	{
		$hidden = new Hidden();
		$hidden->setName("RecordID");
		$html = $hidden->getHTML();
		
		$hidden = new Hidden();
		$hidden->setName("DBTableName");
		$html .= $hidden->getHTML();
		
		$script = new UpdateRecordScript();
		$html .= $script->getHTML();
		$panel = new PanelEditDBRecord($this->Database,'Resources',$this->resourceID);
		$panel->addReadOnlyField('StatusTypeID');
		$panel->setIncludeCommentField(true);
		$panel->setParentPage($this);
		$panel->setTypeTable('ResourceTypeID','RepertoireItems',2);
//		$panel->setYearRange(1400,2020);
		$html .= $panel->getHTML();
		return $html;
		
	}
	

	
}
?>